﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Pagging.ascx.cs" Inherits="Admin_Parts_Pagging" %>
                       
<%
	          int trang = 1;
              int trangBatDau = 1;
              int soDongTrenTrang = 10;
              int tongSoDongDuLieu = 0;
              string LinkPhanTrang = "";
              bool DauTruoc = false;
              bool SauCuoi = false;              
              
              if (Request.QueryString["Page"] != null)
                  trang = Convert.ToInt32(Request.QueryString["Page"].ToString());
              if (Session["SoDongTrenTrang"] != null)
                  soDongTrenTrang = Convert.ToInt32(Session["SoDongTrenTrang"].ToString().Trim());
              if (Session["LinkPhanTrang"] != null)
                  LinkPhanTrang = Session["LinkPhanTrang"].ToString().Trim();
              if (Session["TongSoDongDuLieu"] != null)
                  tongSoDongDuLieu = Convert.ToInt32(Session["TongSoDongDuLieu"].ToString().Trim());

              if (soDongTrenTrang == 0) soDongTrenTrang = 1;
              int soTrang = tongSoDongDuLieu / soDongTrenTrang;
              if (tongSoDongDuLieu % soDongTrenTrang != 0)
                {
                    soTrang++;
                }

              int skip = (trang - 1) * soDongTrenTrang;

              
                int trangKetThuc = soTrang;
                 if(trang==1){
                     if (soTrang <= soDongTrenTrang)
                     {
                        trangBatDau=1;
                        trangKetThuc=soTrang;               
                    }else{
                        trangBatDau=1;
                        trangKetThuc = soDongTrenTrang;
                        DauTruoc=false;
                        SauCuoi=false;
                    }
                    

                 }else if(trang==soTrang){
                     if (soTrang <= soDongTrenTrang)
                     {
                        trangBatDau=1;   
                        trangKetThuc=soTrang;

                       } else{                                        
                            trangBatDau=soTrang-4;
                            trangKetThuc=soTrang;
                             DauTruoc=true;
                             SauCuoi=false;
                       }
                        
                     
                 }else if(trang>1 && trang<soTrang){
                     DauTruoc = false;
                     SauCuoi = false;   
                        if(trang-2>1){
                            trangBatDau=trang-2;                    
                            }else{
                                trangBatDau=1;
                            }

                            if(trang+2<soTrang){
                                trangKetThuc=trang+2;                    
                            }else{
                                trangKetThuc=soTrang;
                                if (soTrang - soDongTrenTrang >= 1)
                                {
                                    trangBatDau = soTrang - (soDongTrenTrang - 1);
                                    DauTruoc=true;
                                    SauCuoi=false;
                                 }
                            }

                            if(trang==2 && trangKetThuc+1<soTrang){
                                trangKetThuc+=1;
                            }

                 }
                 else if (soTrang < soDongTrenTrang)
                 {
                     DauTruoc = false;
                     SauCuoi = false;
                     trangBatDau = 1;
                     trangKetThuc = soTrang;
                 }
              
%>
<div class="pagination">
                     
                      <asp:Label ID="Label1" runat="server" Text="Label">Số lượng hiển thị trên 1 trang</asp:Label>
                      <asp:DropDownList ID="SoSanPhamTrenTrangComboBox" runat="server"                              
                            AutoPostBack="True" 
                          onselectedindexchanged="SoSanPhamTrenTrangComboBox_SelectedIndexChanged">
                            <asp:ListItem Selected="True" Value="10">10</asp:ListItem>                                                     
                            <asp:ListItem Value="20">20</asp:ListItem> 
                            <asp:ListItem Value="30">30</asp:ListItem> 
                            <asp:ListItem Value="40">40</asp:ListItem> 
                            <asp:ListItem Value="50">50</asp:ListItem> 
                            <asp:ListItem Value="60">60</asp:ListItem> 
                            <asp:ListItem Value="70">70</asp:ListItem> 
                            <asp:ListItem Value="80">80</asp:ListItem> 
                            <asp:ListItem Value="90">90</asp:ListItem> 
                            <asp:ListItem Value="100">100</asp:ListItem> 
                        </asp:DropDownList>
                      
    <ul>
     <%    
         
                 
                if(DauTruoc==true){
                    %>
                    <li><a href="<%=LinkPhanTrang %>?Page=<%=1%>" title="">Đầu</a></li>
                    <li><a href="<%=LinkPhanTrang %>?Page=<%=(trang-1)%>" title="">Trước</a></li>
                     <%
                }
                 if (soTrang > 1)
                 {
                       for (int i = trangBatDau; i <= trangKetThuc; i++)
                             {%>
                            <li><a href="<%=LinkPhanTrang %>?Page=<%=i%>" title="" class="number"><%=i%></a></li>
                       <%}
                 }
                
                if(SauCuoi==true){
                    %>
                    <li><a href="<%=LinkPhanTrang %>?Page=<%=(trang+1)%>" title="" >Sau</a></li>
                    <li><a href="<%=LinkPhanTrang %>?Page=<%=soTrang%>" title="">Cuối</a></li>
                     <%
                }%>                

    </ul>
</div>
<!-- End .pagination -->
<div class="clear"></div>